﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Founder.PKURG.Common.DAL
{
    public class ExecuteService
    {
        public ExecuteService()
        {
            if (string.IsNullOrEmpty(DBProcess.dbHelper.ConnectionString))
            {
                DBProcess.dbHelper.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
            }
        }

        public ExecuteService(string connectionString)
        {
            DBProcess.dbHelper.ConnectionString = connectionString;
            if (string.IsNullOrEmpty(DBProcess.dbHelper.ConnectionString))
            {
                DBProcess.dbHelper.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
            }
        }

        public ReturnInfo GetValueRequest(string requestName, object[] parms)
        {
            return CommonRequest(ReturnDataType.单值, requestName, parms);
        }
        public ReturnInfo GetObjectRequest(string requestName, object[] parms)
        {
            return CommonRequest(ReturnDataType.对象, requestName, parms);
        }
        public ReturnInfo GetListRequest(string requestName, params object[] parms)
        {
            return CommonRequest(ReturnDataType.列表, requestName, parms);
        }

        public ReturnInfo CommonRequest(ReturnDataType requestType, string requestName, params object[] parms)
        {
            ReturnInfo response = new ReturnInfo();
            try
            {
                DataTable dt = DBProcess.GetDataTable(requestName, parms);
                switch (requestType)
                {
                    case ReturnDataType.单值:
                        if (dt.Rows.Count > 0)
                        {
                            response.MainData = dt.Rows[0][0].ToString();
                        }
                        break;
                    case ReturnDataType.对象:
                        response.MainData = Founder.PKURG.Common.Core.CustomMapping.GetObject(dt);
                        break;
                    case ReturnDataType.列表:
                        response.MainData = Founder.PKURG.Common.Core.CustomMapping.GetList(dt);
                        break;
                    default:
                        break;
                }
                response.returnValue = true;
            }
            catch (Exception ex)
            {
                response.message = ex.Message;
                response.returnValue = false;
            }
            return response;
        }
    }
}
